package com.service.infoEntry.service.impl;

import com.service.config.utils.DateUtils;
import com.service.config.utils.IdUtil;
import com.service.config.utils.ModelMapperUtil;
import com.service.infoEntry.dao.CommentDao;
import com.service.infoEntry.dao.TransactionCommentDao;
import com.service.infoEntry.dto.CommentDto;
import com.service.infoEntry.dto.TransactionCommentDto;
import com.service.infoEntry.entity.CommentInfo;
import com.service.infoEntry.entity.TransactionCommentInfo;
import com.service.infoEntry.service.TransactionCommentService;
import com.service.infoEntry.service.UserInfoService;
import com.service.infoEntry.vo.TransactionCommentVo;
import com.service.infoEntry.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TransactionCommentServiceImpl implements TransactionCommentService {
    @Resource
    private TransactionCommentDao transactionCommentDao;
    @Resource
    private UserInfoService userService;

    @Override
    public void addComment(TransactionCommentDto transactionCommentDto) {
        TransactionCommentInfo commentInfo = ModelMapperUtil.strictMap(transactionCommentDto, TransactionCommentInfo.class);
        commentInfo.setId(IdUtil.getStringId());
        commentInfo.setAddTime(DateUtils.getNowDate());
        UserVo userVo = userService.queryByUserId(transactionCommentDto.getUserId());
        commentInfo.setUsername(userVo.getUsername());
        transactionCommentDao.addComment(commentInfo);
    }

    @Override
    public List<TransactionCommentVo> queryAllCommentByTransactionId(String transactionId, String userId) {
        List<TransactionCommentVo> transactionCommentVos = new ArrayList<>();
        List<TransactionCommentInfo> transactionCommentInfos = transactionCommentDao.queryAllCommentByTransactionId(transactionId);
        TransactionCommentVo transactionCommentVo;
        UserVo userVo;
        for (TransactionCommentInfo transactionCommentInfo : transactionCommentInfos) {
            transactionCommentVo = ModelMapperUtil.strictMap(transactionCommentInfo,TransactionCommentVo.class);
            userVo=userService.queryByUserId(transactionCommentInfo.getUserId());
            if(transactionCommentInfo.getUserId().equals(userId))
            {
                userVo.setCurrentUser(true);
            }
            if (userVo.getFullName() == null) {
                userVo.setFullName("");
            }
            transactionCommentVo.setUserVo(userVo);
            transactionCommentVos.add(transactionCommentVo);
        }

        return transactionCommentVos;
    }
    @Override
    public void deleteCommentByCommentId(String commentId) {
        transactionCommentDao.deleteCommentByCommentId(commentId);
    }

}
